<!DOCTYPE html>
<html><head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta name="google" content="notranslate">
  <title>嵌入式Server和Client通信实验 &middot; Dragon Blog</title>
  <meta name="keywords" content="素锦, inspiration, customization, rainmeter, design, web, 壁纸, 设计, 收集, wallpaper, collection, jaku, icon">
  <meta name="description" content="世界上每个角落都有人过着相似的人生">
  <meta name="author" content="CodeLong">
  <link rel="icon" type="image/png" href="">
  <link rel="stylesheet" href="/css/diaspora.css">
  <link rel="stylesheet" href="/css/insight.css">
  <link rel="stylesheet" href="/css/custom.css">
</head><body class="loading">
        <div id="loading"></div>
				<div id="nav"></div>
				<div class="nav-user"></div>
    <div id="single">
    <div id="top" style="display: block;">
        <div class="bar">
        </div>
        <a class="icon-icon" href="javascript:history.back()">
        </a>
        <div title="播放/暂停" class="icon-play">
        </div>
        
        <h3 class="subtitle" style="display: none;">
        嵌入式Server和Client通信实验</h3>
        <div class="social">
            <div>
                <div class="share">
                    <a title="获取二维码" class="icon-wechat" href="javascript:;"></a>
                </div>
                <div id="qr"></div>
            </div>
        </div>
        <div class="scrollbar" style="width: 1.1636%;"></div>
    </div>
    <div class="section">
        <div class="article">
            <div>
                <h1 class="title">
                嵌入式Server和Client通信实验</h1>
                <div class="stuff">
                    
                    <span>November 21, 2019</span>
                    <span>字数 1300</span>
                    
                    
                </div>
                <div class="content">
                    

<h1 id="unbuntu系统和树莓派通信实验步骤">unbuntu系统和树莓派通信实验步骤</h1>

<p>最近老师给我们布置了一个实验，是利用ubuntu和树莓派进行客户端和服务器端的通信，下面我就演示一下这个实验的步骤吧</p>

<h2 id="树莓派的配置">树莓派的配置</h2>

<p><img src="http://shumeipai.nxez.com/wp-content/uploads/2018/03/20180314183505975-0.jpg" alt="树莓派" title="树莓派" />
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;<u>树莓派</u></p>

<h5 id="第一步-下载并安装树莓派系统">第一步：下载并安装树莓派系统</h5>

<p>前往<a href="https://www.raspberrypi.org/downloads/raspbian/">树莓派的官网</a>，下载raspbian系统
<img src="https://note.youdao.com/yws/api/personal/file/WEB06a92e8f7e71f8574ffc8652a3fc4318?method=download&amp;shareKey=b3cb2f12fb4b325f75744853f8156b44" alt="download" />
下载完，你会得到一个.img的文件，这个就是系统的镜像！如果不是，请解压你下载的压缩包。</p>

<p>烧制系统，我推荐用<a href="https://balenaetcher.en.softonic.com/">balenaEtcher</a>这个软件，方便简单
<img src="https://note.youdao.com/yws/api/personal/file/WEBb251f17af81107fccfe0557f8be02d90?method=download&amp;shareKey=22b995e75e88e2a7961e621c6b0bf2dd" alt="" /></p>

<h5 id="第二步-配置树莓派">第二步：配置树莓派</h5>

<p>因为考虑到许多人没有HDMI接口线，而且也没有显示器。我们接下来就用一种简单的、不需要其他工具的方法来连接树莓派。
烧制系统完成后，我先要在树莓派做一些配置。主要是让树莓派开机自动开启<strong>ssh</strong>和<strong>wifi</strong>服务。</p>

<p>首先我们现在sd卡的目录下，新建一个名为<strong>ssh</strong>的txt文本。</p>

<p>然后，我们在目录下新建一个名为<strong>wpa_supplicant.conf</strong>的空白文件。
我是直接使用<strong>cmd</strong>新建的。
<img src="https://note.youdao.com/yws/api/personal/file/WEBa5f0dce7eff2e3d9b9a0876975b2e8c9?method=download&amp;shareKey=df35b088b8dbae4c142aa78ebac28d9b" alt="" />
然后用记事本打开这个文件，在文件中输入以下代码：</p>

<pre><code>country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
	ssid=&quot;WIFI1&quot;
	psk=&quot;123456789&quot;
	priority=2
}
</code></pre>

<p>WIFI1是你的wifi名字，psk是的wifi密码。这个根据你的实际情况修改。
为什么要用到wifi呢？因为这个实验要在<strong>同一局域网</strong>实现，最简单的方法就是手机开wifi，让树莓派和ubuntu连接到同一个网络中。这个下面再详细说。</p>

<h5 id="第三步-开启树莓派">第三步：开启树莓派</h5>

<p>首先打开手机wifi，然后让你的电脑连接上你的wifi，确定ubuntu可以上网后。</p>

<p>接下来，我们为树莓派通电，等待30s，因为初次系统启动会比较慢。
<img src="https://note.youdao.com/yws/api/personal/file/WEBfe17b82732ee68d225938de251b4a843?method=download&amp;shareKey=f83ce2d60f62d0f9f58b9935fc04a789" alt="" /></p>

<p><img src="https://note.youdao.com/yws/api/personal/file/WEBa97fff4ed0c423c51f600441c6583483?method=download&amp;shareKey=398e12ab9c98b33fbb20b789a1c6ce0b" alt="" /></p>

<p>发现树莓派上线后，就可使用<strong>putty</strong>用刚才的IP地址进行连接。<strong>putty</strong>是终端画面来的，如果想要可视化界面，建议可以安装<strong>VNC viewer</strong>
<img src="https://note.youdao.com/yws/api/personal/file/WEB9d9f7a9d1044a306b6d93532eb6dde24?method=download&amp;shareKey=979b74dbf13acc49a47fed346fae80d0" alt="" />
&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;<u><strong>putty</strong></u></p>

<h2 id="客户端和服务端">客户端和服务端</h2>

<p>我们先在ubuntu建立服务器</p>

<p>直接输入刚刚的IP地址就可以连接啦！
树莓派的初始账号是pi，密码是raspberry。然后就顺利进入终端啦！
<img src="https://note.youdao.com/yws/api/personal/file/WEB3fa70ba696b39bd06af1c35ec64bf531?method=download&amp;shareKey=e5d80fb05db24d119c4fdef8f4644495" alt="" /></p>

<p>然后我们在树莓派输入以下命令：
<img src="https://note.youdao.com/yws/api/personal/file/WEB101f12e5e3b47ba91ccde29c5305114a?method=download&amp;shareKey=2d6e315e506f3e7b14a80bf1f24f6a3c" alt="" /></p>

<p>在Ubuntu输入以下命令：
<img src="https://note.youdao.com/yws/api/personal/file/WEB91971ca55fcb8e7fb992fcac9b57a2e5?method=download&amp;shareKey=d43ef949b2b9112c0929b9b3574b172f" alt="" /></p>

<p>先在树莓派上开启服务器端
<img src="https://note.youdao.com/yws/api/personal/file/WEB4787363649f4c6776a7779350b878e4c?method=download&amp;shareKey=af2ffab931650b7f9603cdfc30bc7edf" alt="" /></p>

<p>在Ubuntu上开启客户端，并发送信息
<img src="https://note.youdao.com/yws/api/personal/file/WEB156b136272d8e2f18f1ac25ed783fe94?method=download&amp;shareKey=9a012b602f9fc3a9fa34344a3a878c18" alt="" /></p>

<p>服务器端已经接收到信息
<img src="https://note.youdao.com/yws/api/personal/file/WEBeed926265e6c3b9b276d60b647f6a7b5?method=download&amp;shareKey=818b63fd119294eac2e7488ba4c0cce9" alt="" /></p>

<p><strong>Server端代码</strong></p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt; 
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc &lt; 2) {
         fprintf(stderr,&quot;ERROR, no port provided\n&quot;);
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd &lt; 0) 
        error(&quot;ERROR opening socket&quot;);
     bzero((char *) &amp;serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,
              sizeof(serv_addr)) &lt; 0) 
              error(&quot;ERROR on binding&quot;);

     while (1) {
          listen(sockfd,5);
          clilen = sizeof(cli_addr);
          newsockfd = accept(sockfd, 
                      (struct sockaddr *) &amp;cli_addr, 
                      &amp;clilen);
          if (newsockfd &lt; 0) 
               error(&quot;ERROR on accept&quot;);

      bzero(buffer,256);
          n = read(newsockfd,buffer,255);
      if (n &lt; 0) error(&quot;ERROR reading from socket&quot;);
      printf(&quot;Here is the message: %s&quot;,buffer);
      n = write(newsockfd,&quot;I got your message&quot;,18);
      if (n &lt; 0) error(&quot;ERROR writing to socket&quot;);   
     }

     close(newsockfd);
     close(sockfd);
     return 0; 
}

</code></pre>

<p><strong>Client端代码</strong></p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;netdb.h&gt; 

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc &lt; 3) {
       fprintf(stderr,&quot;usage %s hostname port\n&quot;, argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd &lt; 0) 
        error(&quot;ERROR opening socket&quot;);
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,&quot;ERROR, no such host\n&quot;);
        exit(0);
    }
    bzero((char *) &amp;serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server-&gt;h_addr, 
         (char *)&amp;serv_addr.sin_addr.s_addr,
         server-&gt;h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0) 
        error(&quot;ERROR connecting&quot;);
    printf(&quot;Please enter the message: &quot;);
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if (n &lt; 0) 
         error(&quot;ERROR writing to socket&quot;);
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n &lt; 0) 
         error(&quot;ERROR reading from socket&quot;);
    printf(&quot;%s\n&quot;,buffer);
    close(sockfd);
    return 0;
}

</code></pre>

<p>Author：「CodeLong」<br>
BGM：告白之夜</p>

                    
                    <audio id="audio" loop="1" preload="auto" style="width: 100%;" controls="controls">
                        <source type="audio/mpeg" src="/mp3/%e5%91%8a%e7%99%bd%e4%b9%8b%e5%a4%9c.mp3">
                        <a href="/mp3/%e5%91%8a%e7%99%bd%e4%b9%8b%e5%a4%9c.mp3">/mp3/告白之夜.mp3</a>
                    </audio>
                </div>
                <div class="comment-wrap">

                </div>
            </div>
        </div>
    </div>
    <div class="relate">
        <ul>
            <h3 id="prev_next">
                <em>相 关 文 章</em>
                <span>
                    <a href="javascript: window.scrollTo(0, 0);">
                    返回顶部</a>
                    
                        <a href="https://codelong2010.github.io/posts/%E6%AC%A2%E8%BF%8E%E5%A4%A7%E5%AE%B6%E6%9D%A5%E6%88%91%E7%9A%84%E5%8D%9A%E5%AE%A2/" rel="prev">上一篇</a>
                    
                    
                        <a href="https://codelong2010.github.io/posts/%E6%A0%91%E8%8E%93%E6%B4%BE%E4%BA%BA%E8%84%B8%E6%A3%80%E6%B5%8B/" rel="next">下一篇</a>
                    
                </span>
            </h3>
            
            
            
                
                    
                    <li>
                        <div>
                            <a class="relatea" href="https://codelong2010.github.io/posts/%E6%A0%91%E8%8E%93%E6%B4%BE%E4%BA%BA%E8%84%B8%E6%A3%80%E6%B5%8B/" title="python-opencv实现人脸检测">
                            python-opencv实现人脸检测</a>
                            <p>喜欢我的小伙伴，请多点关注我哦！...</p>
                        </div>
                        
                        <a href="https://codelong2010.github.io/posts/%E6%A0%91%E8%8E%93%E6%B4%BE%E4%BA%BA%E8%84%B8%E6%A3%80%E6%B5%8B/" title="python-opencv实现人脸检测">
                            <img class="relateimg" src="/img/2.jpg" alt="python-opencv实现人脸检测">
                        </a>
                    </li>
                    
                
                    
                
                    
                    <li>
                        <div>
                            <a class="relatea" href="https://codelong2010.github.io/posts/%E6%AC%A2%E8%BF%8E%E5%A4%A7%E5%AE%B6%E6%9D%A5%E6%88%91%E7%9A%84%E5%8D%9A%E5%AE%A2/" title="欢迎来到CodeLong的博客！">
                            欢迎来到CodeLong的博客！</a>
                            <p>这是我第一次搭建博客！成功了，很开心！嘻嘻！以后我也会在博客上分享一些学习和生活上的感悟！希望大家来多多支持哦！...</p>
                        </div>
                        
                        <a href="https://codelong2010.github.io/posts/%E6%AC%A2%E8%BF%8E%E5%A4%A7%E5%AE%B6%E6%9D%A5%E6%88%91%E7%9A%84%E5%8D%9A%E5%AE%A2/" title="欢迎来到CodeLong的博客！">
                            <img class="relateimg" src="/img/face.jpg" alt="欢迎来到CodeLong的博客！">
                        </a>
                    </li>
                    
                
            
        </ul>
    </div>
</div>
<p style="text-align: center;">
  <a style="color: inherit" target="_blank" href="https://github.com/honjun/hugo-theme-diaspora"></a>
</p>

<script>
  var siteTitle = "Dragon Blog";
</script>
<script src="/js/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/diaspora.js"></script>
<script src="/js/custom.js"></script>
<script src="/js/InsightSearch.js"></script>
</body>
</html>

